<!-- 查询用户信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="手机号">
                            <a-input v-model="phone" allow-clear placeholder="请输入用户手机号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="姓名">
                            <a-input v-model="userName" allow-clear placeholder="请输入用户姓名" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户ID">
                            <a-input v-model="userId" allow-clear placeholder="请输入用户ID" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="花店名称">
                            <a-input v-model="shopName" allow-clear placeholder="请输入花店名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="省份">
                            <a-input v-model="province" allow-clear placeholder="请输入省份" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="城市">
                            <a-input v-model="city" allow-clear placeholder="请输入城市" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="区域">
                            <a-input v-model="country" allow-clear placeholder="请输入区域，例：徐汇区" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户等级">
                            <a-select v-model="grade" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in 10" :key="i" :value="i">{{i === 0 ? '普通会员' : `${i}级`}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <!-- <a-col :md="12" :sm="24">
                        <a-form-item label="注册时间">
                            <a-range-picker @change="onChange" allowClear/>
                        </a-form-item>
                    </a-col> -->
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" @click="getData()">查询</a-button>
                            <!-- <a-button style="margin-left: 20px" type="" icon="upload" @click="downloadData()">导出数据</a-button> -->
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="userId" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small" :scroll="{ x: 1500 }" >
            <div slot="grade" slot-scope="grade">
                <div v-for="(item) in gradeList" :key="item.id">
                    <span v-if="item.grade == grade">{{item.gradeName}}</span>
                </div>
                <!-- <div>{{grade === 0 ? '普通会员' : `${grade}级`}}</div> -->
            </div>
            <template slot="operation" slot-scope="text, record">
                <a-button type="link" @click="relationEvt(record,1)">上级</a-button>
                <a-button type="link" @click="relationEvt(record,2)">下级</a-button>
            </template>
            <template slot="province" slot-scope="province, record">
                <div>{{province}}-{{record.city}}-{{record.country}}</div>
            </template>
            <template slot="gradeOperation" slot-scope="text, record">
                <a-button type="link" @click="updateGrade(record)">修改</a-button>
            </template>
            <template slot="detail" slot-scope="text, record">
                <a-button type="link" @click="stat(record)">查看</a-button>
            </template>
            <template slot="sonVipOrder" slot-scope="text, record">
                <a-button type="link" @click="sonVipOrder(record)">查看</a-button>
            </template>
            <template slot="userOperation" slot-scope="text, record">
                <a-button type="link" @click="userInfoDetail(record)">查看</a-button>
            </template>
            <template slot="realNameOperation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="updateUserRealName(record)">修改</a-button>
                    <a-popconfirm
                        v-if="record.userName != '' && record.userName"
                        title="是否删除当前用户实名信息?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="delUserRealName(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="查询用户关系"
            :visible="visible"
            width="80%"
            @ok="visible = false"
            @cancel="visible = false"
            >
            <div>
                <relation :parent="parent" :brandId="brandId" />
            </div>
        </a-modal>

        <!-- 修改用户等级 -->
        <a-modal
            title="修改用户等级"
            :visible="gradeVisible"
            width="80%"
            @ok="setGrade"
            @cancel="gradeVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户姓名">
                        <a-input v-model="user.userName" disabled placeholder="请输入用户姓名" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户手机号">
                        <a-input v-model="user.phone" disabled placeholder="请输入用户手机号" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户等级">
                        <a-select v-model="user.grade" placeholder="请选择需要修改的等级" allowClear @change="getData">
                            <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 修改用户实名信息 -->
        <a-modal
            title="修改用户实名信息"
            :visible="realNameVisible"
            width="80%"
            @ok="updateUserRealNameHandle"
            @cancel="realNameVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户号码">
                        <a-input v-model="user.phone" disabled placeholder="请输入用户号码" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户姓名">
                        <a-input v-model="user.userName" placeholder="请输入用户姓名" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户身份证号">
                        <a-input v-model="user.idNumber" placeholder="请输入用户身份证号" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 导出数据 -->
        <a-modal
            title="导出数据，每页1000条"
            :visible="downloadVisible"
            @ok="downladHandleOk"
            :confirm-loading="confirmLoading"
            @cancel="downloadVisible = false"
            >
            <div>
                <a-pagination v-model="current" :page-size.sync="pageSize" :total="total" show-less-items />
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryMember , updateUserFarmerGrade , queryFarmerUser , downloadMember , updateUserRealName } from '@/api/member';
import { queryGradeProduct } from '@/api/product';
import relation from './components/relation';
import { setUserIgnorePassword } from '@/api/power';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            isload: false,
            dataList: [],
            columns,
            total: 0,
            page: 1,
            size: 10,
            phone: '',
            userName: '',
            userId: '',
            shopName: '', // 花店名称
            province: '', // 省份
            city: '', // 城市
            country: '', // 区域
            gtCreateDate: '', // 开始时间
            ltCreateDate: '', // 结束时间
            grade: undefined,
            gradeList: [],
            visible: false,
            parent: {},
            visibleFilecoinAddress: false,
            filecoinAddressPhone: '',
            filecoinAddressBrandId: '',
            gradeVisible: false,
            user: {
                idNumber: '',
                userName: '',
                phone: '',
                userId: '',
            },
            confirmLoading: false,
            downloadVisible: false,
            pageSize: 1000,
            current: 1,
            realNameVisible: false,
        };
    },
    components: {
        relation,
    },
    async created(){
        try {
            this.gradeList = await this.getGrade()
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        onChange(date, dateString) {
            console.log(date, dateString);
            this.gtCreateDate = dateString[0]
            this.ltCreateDate = dateString[1]
            this.getData()
        },
        // 查询等级信息
        getGrade(brandId){
            let _this = this
            return new Promise((resolve,reject)=>{
                const params = {
                    'brandId':brandId,
                    'size':99,
                    'page':0,
                }
                queryGradeProduct(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data.content)
                    }else{
                        reject()
                    }
                })
            })
        },
        // 查询会员信息
        getData(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'phone':_this.phone,
                'userId':_this.userId,
                'userName': _this.userName,
                'shopName': _this.shopName,
                'province': _this.province,
                'city': _this.city,
                'country': _this.country,
                'grade':_this.grade,
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryMember(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 查看用户关系
        relationEvt(item,type){
            if(type == 1){
                // 查看上级
                this.parent = new Object({
                    sonUserId: item.userId,
                    parentUserId: '',
                })
                this.visible = true
            }else{
                // 查看下级
                this.parent = new Object({
                    sonUserId: '',
                    parentUserId: item.userId,
                })
                this.visible = true
            }
        },
        // 查看用户FIL钱包地址
        filecoinAddressEvt(item){
            this.filecoinAddressPhone = item.phone
            this.filecoinAddressBrandId = item.brandId
            this.visibleFilecoinAddress = true
        },
        // 修改用户等级
        async updateGrade(user){
            this.user = JSON.parse(JSON.stringify(user))
            // this.gradeVisible = true
            this.gradeList = await this.getGrade(this.user.brandId)
            this.gradeVisible = true
        },
        // 设置用户等级
        setGrade(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'phone':_this.user.phone,
                'grade':_this.user.grade,
                // 'userId':_this.user.userId,
            }
            updateUserFarmerGrade(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.gradeVisible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        stat(item){
            this.$router.push({ path: '/member/stat',query:{userId:item.userId,phone:item.phone,userName:item.userName }})
        },
        sonVipOrder(item){
            this.$router.push({ path: '/member/sonVipOrder',query:{brandId:item.brandId,phone:item.phone,userName:item.userName }})
        },
        // 查看个人信息
        userInfoDetail(item){
            this.$router.push({ path: '/member/index/userInfo',query:{brandId:item.brandId,user:JSON.stringify(item) }})
        },
        // 代操作
        userOperation(user){
            console.log(process.env,'process')
            let url = ''
            const params = {
                account: user.phone,
                brandId: user.brandId
            }
            setUserIgnorePassword(params)
            .then(res=>{
                if(res.success){
                    if(process.env.VUE_APP_API_BASE_URL == '/fileCoinGKApi'){
                        url = 'https://h5.gushouipo.com/xingyaogaoke/xygkNew/#/?brandId=' + user.brandId + '&userId=' + user.userId
                    }else{
                        url = 'https://h5.gushouipo.com/xingyaogaoke/xygkNewtest/#/?brandId=' + user.brandId + '&userId=' + user.userId
                    }
                    window.open(url,'代操作','width=375,height=700,top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
            
        },
        // 导出数据
        downloadData(){
            this.downloadVisible = true
        },
        downladHandleOk(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'phone':_this.phone,
                'userId':_this.userId,
                'userName': _this.userName,
                'grade':_this.grade,
                'isDownload': '1',
                'size':_this.pageSize,
                'page':_this.current - 1,
            }
            downloadMember(params)
            .then(res=>{
                _this.isload = false
                if(res){
                    _this.downloadVisible = false;
                    const content = res
                    console.log(res,"res")
                    let blob = new Blob([content], {type: 'application/x-excel'});
                    const fileName = '会员信息列表.xlsx'
                    if ('download' in document.createElement('a')) { // 非IE下载
                        const elink = document.createElement('a')
                        elink.download = fileName
                        elink.style.display = 'none'
                        elink.href = URL.createObjectURL(blob)
                        document.body.appendChild(elink)
                        elink.click()
                        URL.revokeObjectURL(elink.href) // 释放URL 对象
                        document.body.removeChild(elink)
                    } else { // IE10+下载
                        navigator.msSaveBlob(blob, fileName)
                    }
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 修改用户实名信息
        updateUserRealName(item){
            this.user = JSON.parse(JSON.stringify(item));
            this.realNameVisible = true;
        },
        updateUserRealNameHandle(){
            let _this = this;
            if(_this.user.realName == ''){
                _this.$message.warning('请输入用户姓名');
                return;
            }
            if(_this.user.idNumber == ''){
                _this.$message.warning('请输入用户身份证号码');
                return;
            }
            const params = {
                'userId': _this.user.userId,
                'realName': _this.user.userName,
                'idNumber': _this.user.idNumber,
                'phone': _this.user.phone,
            }
            updateUserRealName(params)
            .then(res => {
                if(res.success){
                    _this.realNameVisible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除用户实名信息
        delUserRealName(item){
            let _this = this;
            const params = {
                'userId': item.userId,
                'isDelete': 1,
            }
            updateUserRealName(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>